﻿<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="keywords" content="个人博客模板,博客模板,响应式" />
<meta name="description" content="如影随形主题的个人博客模板，神秘、俏皮。" />
<!--头部开始-->
 <link href="http://127.0.0.1/cms/cms/Template/black/css/base.css" rel="stylesheet">
<link href="http://127.0.0.1/cms/cms/Template/black/css/index.css" rel="stylesheet">
<link href="http://127.0.0.1/cms/cms/Template/black/css/media.css" rel="stylesheet">
<link href="http://127.0.0.1/cms/cms/Template/black/css/style.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<!--[if lt IE 9]>
<script src="js/modernizr.js"></script>
<![endif]-->
</head>

<body>
	<div class="ibody">
		<header>
			<h1>邦CMS</h1>
			<h2>我的生活像 拍了一出戏 有超多导演跟编剧</h2>
			<nav id="topnav">
				<a href="http://127.0.0.1/cms/cms/index.php">首页</a>
					
					<a href="http://127.0.0.1/cms/cms/index.php/Index/list/index/cid/24">互联网新闻</a>
				
	
					<a href="http://127.0.0.1/cms/cms/index.php/Index/list/index/cid/25">前端技术</a>
				
	
					<a href="http://127.0.0.1/cms/cms/index.php/Index/list/index/cid/26">程序员生活</a>
				
	
					<a href="http://127.0.0.1/cms/cms/index.php/Index/list/index/cid/27">编程开发</a>
				
	
					<a href="http://127.0.0.1/cms/cms/index.php/Index/list/index/cid/28">视频</a>
				
			</nav>
		</header>
<!--头部结束-->
  <article>
    <h2 class="about_h"><!--您现在的位置是：<a href="/">首页</a>><a href="1/">模板分享</a>><a href="1/">个人博客模板</a>--></h2>
    <div class="index_about">
      <h2 class="c_titile">PHP数组操作详解</h2>
      <p class="box_c"><span class="d_time">发布时间：2015-06-09</span><span>作者：segmentfault</span><span>浏览（131）</span></p>
      <ul class="infos">
        <h2 id="articleHeader1">数组的基本操作</h2><h3 id="articleHeader2">php定义数组：</h3><h3 id="articleHeader2">&nbsp;</h3><pre class="hljs xml">&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$array&nbsp;=&nbsp;array();&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$array[&quot;key&quot;]&nbsp;=&nbsp;&quot;values&quot;;&nbsp;&nbsp;
?&gt;</pre><p>在<code>PHP</code>中声明数组的方式主要有两种：</p><p>1.用<code>array()</code>函数声明数组，<br/>2.直接为数组元素赋值。</p><pre class="hljs xml">&nbsp;&nbsp;&nbsp;&nbsp;//array数组
&nbsp;&nbsp;&nbsp;&nbsp;$users&nbsp;=&nbsp;array(&#39;phone&#39;,&#39;computer&#39;,&#39;dos&#39;,&#39;linux&#39;);&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$users;//只会打印出数据类型Array
&nbsp;&nbsp;&nbsp;&nbsp;print_r($users);//Array&nbsp;(&nbsp;[0]&nbsp;=&gt;&nbsp;phone&nbsp;[1]&nbsp;=&gt;&nbsp;computer&nbsp;[2]&nbsp;=&gt;&nbsp;dos&nbsp;[3]&nbsp;=&gt;&nbsp;linux&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;$numbers&nbsp;=&nbsp;range(1,5);//创建一个包含指定范围的数组
&nbsp;&nbsp;&nbsp;&nbsp;print_r($numbers);//Array&nbsp;(&nbsp;[0]&nbsp;=&gt;&nbsp;1&nbsp;[1]&nbsp;=&gt;&nbsp;2&nbsp;[2]&nbsp;=&gt;&nbsp;3&nbsp;[3]&nbsp;=&gt;&nbsp;4&nbsp;[4]&nbsp;=&gt;&nbsp;5&nbsp;)
&nbsp;&nbsp;&nbsp;&nbsp;print_r(true);//1
&nbsp;&nbsp;&nbsp;&nbsp;var_dump(false);//bool(false)//print_r可以把字符串和数字简单地打印出来，数组会以Array开头并已键值形式表示，print_r输出布尔值和null的结果没有意义，因此用var_dump更合适//通过循环来显示数组里所有的值
&nbsp;&nbsp;&nbsp;&nbsp;for($i&nbsp;=&nbsp;0&nbsp;;$i&nbsp;&lt;&nbsp;5;$i++){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$users[$i];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;
&#39;;
&nbsp;&nbsp;&nbsp;&nbsp;}//通过count/sizeof统计数组中单元数目或对象中的属性个数

&nbsp;&nbsp;&nbsp;&nbsp;for($i&nbsp;=&nbsp;0;&nbsp;$i&nbsp;&lt;&nbsp;count($users);$i++){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$users[$i];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;
&#39;;
&nbsp;&nbsp;&nbsp;&nbsp;}//还可以通过foreach循环来遍历数组，这种好处在于不需要考虑key
&nbsp;&nbsp;&nbsp;&nbsp;foreach($users&nbsp;as&nbsp;$value){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$value.&#39;
&#39;;//点号为字符串连接符号
&nbsp;&nbsp;&nbsp;&nbsp;}//foreach循环遍历&nbsp;$key&nbsp;=&gt;&nbsp;$value；$key和$value是变量名，可以自行设置
&nbsp;&nbsp;&nbsp;&nbsp;foreach($users&nbsp;as&nbsp;$key&nbsp;=&gt;&nbsp;$value){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$key.&#39;
&#39;;//输出键
&nbsp;&nbsp;&nbsp;&nbsp;}?&gt;</pre><h3 id="articleHeader3">创建自定义键的数组</h3><h3>&nbsp;</h3><pre class="hljs xml">&nbsp;&nbsp;&nbsp;&nbsp;//创建自定义键的数组
&nbsp;&nbsp;&nbsp;&nbsp;$ceo&nbsp;=&nbsp;array(&#39;apple&#39;=&gt;&#39;jobs&#39;,&#39;microsoft&#39;=&gt;&#39;Nadella&#39;,&#39;Larry&nbsp;Page&#39;,&#39;Eric&#39;);&nbsp;&nbsp;&nbsp;&nbsp;//如果不去声明元素的key,它会从零开始
&nbsp;&nbsp;&nbsp;&nbsp;print_r($ceo);//Array&nbsp;(&nbsp;[apple]&nbsp;=&gt;&nbsp;jobs&nbsp;[microsoft]&nbsp;=&gt;&nbsp;Nadella&nbsp;[0]&nbsp;=&gt;&nbsp;Larry&nbsp;Page&nbsp;[1]&nbsp;=&gt;&nbsp;Eric&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$ceo[&#39;apple&#39;];//jobs

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//php5.4起的用法
&nbsp;&nbsp;&nbsp;&nbsp;$array&nbsp;=&nbsp;[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;foo&quot;&nbsp;=&gt;&nbsp;&quot;bar&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;bar&quot;&nbsp;=&gt;&nbsp;&quot;foo&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;];

&nbsp;&nbsp;&nbsp;&nbsp;print_r($array);//Array&nbsp;(&nbsp;[foo]&nbsp;=&gt;&nbsp;bar&nbsp;[bar]&nbsp;=&gt;&nbsp;foo&nbsp;)&nbsp;?&gt;</pre><p>从<code>php5.4</code> 起可以使用短数组定义语法，用 <code>[]</code> 替代 <code>array()</code>。有点类似于<code>javascript</code>中数组的定义。</p><h3 id="articleHeader4">each()的使用</h3><h3>&nbsp;</h3><pre class="hljs xml">&nbsp;&nbsp;&nbsp;&nbsp;//通过为数组元素赋值来创建数组
&nbsp;&nbsp;&nbsp;&nbsp;$ages[&#39;trigkit4&#39;]&nbsp;=&nbsp;22;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$ages.&#39;
&#39;;//Array
&nbsp;&nbsp;&nbsp;&nbsp;//因为相关数组的索引不是数字，所以无法通过for循环来进行遍历操作，只能通过foreach循环或list()和each()结构

&nbsp;&nbsp;&nbsp;&nbsp;//each的使用
&nbsp;&nbsp;&nbsp;&nbsp;//each返回数组中当前的键/值对并将数组指针向前移动一步
&nbsp;&nbsp;&nbsp;&nbsp;$users&nbsp;=&nbsp;array(&#39;trigkit4&#39;=&gt;22,&#39;mike&#39;=&gt;20,&#39;john&#39;=&gt;30);&nbsp;&nbsp;&nbsp;&nbsp;//print_r(each($users));//Array&nbsp;(&nbsp;[1]&nbsp;=&gt;&nbsp;22&nbsp;[value]&nbsp;=&gt;&nbsp;22&nbsp;[0]&nbsp;=&gt;&nbsp;trigkit4&nbsp;[key]&nbsp;=&gt;&nbsp;trigkit4&nbsp;)

&nbsp;&nbsp;&nbsp;//相当于：$a&nbsp;=&nbsp;array([0]=&gt;trigkit4,[1]=&gt;22,[value]=&gt;22,[key]=&gt;trigkit4);
&nbsp;&nbsp;&nbsp;&nbsp;$a&nbsp;=&nbsp;each($users);//each把原来的数组的第一个元素拿出来包装成新数组后赋值给$a
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$a[0];//trigkit4

&nbsp;&nbsp;&nbsp;&nbsp;//!!表示将真实存在的数据转换成布尔值
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;!!each($users);//1?&gt;</pre><p><code>each</code>的指针指向第一个键值对，并返回第一个数组元素，获取其键值对，并包装成新数组</p><h3 id="articleHeader5">list()的使用</h3><h3>&nbsp;</h3><p><code>list</code>用来把数组用的值赋给一些变量，看下面例子：</p><pre class="hljs xml">&nbsp;&nbsp;&nbsp;&nbsp;$a&nbsp;=&nbsp;[&#39;2&#39;,&#39;abc&#39;,&#39;def&#39;];&nbsp;&nbsp;&nbsp;&nbsp;list($var1,$var2)&nbsp;=&nbsp;$a;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$var1.&#39;
&#39;;//2
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$var2;//abc

&nbsp;&nbsp;&nbsp;&nbsp;$a&nbsp;=&nbsp;[&#39;name&#39;=&gt;&#39;trigkit4&#39;,&#39;age&#39;=&gt;22,&#39;0&#39;=&gt;&#39;boy&#39;];&nbsp;&nbsp;&nbsp;&nbsp;//list只认识key为数字的索引
&nbsp;&nbsp;&nbsp;&nbsp;list($var1,$var2)&nbsp;=&nbsp;$a;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$var1;//boy?&gt;</pre><p>注：<code>list</code>只认识key为数字的索引</p><p><br/></p>      </ul>
      <div class="keybq">
        <p><span>关键字词</span>：
        					技巧							学习			        </p>
      </div>
      
      <div class="otherlink">
        <h2>相关文章</h2>
        <ul>
          	
          <li><a href="http://127.0.0.1/cms/cms/Static/black/PHP/29.html" title="PHP数组操作详解">PHP数组操作详解</a></li>
          
        </ul>
      </div>
    </div>
  </article>
 <!--右边开始-->
  <aside>
    <div class="avatar"><a href="about.html"><span>关于邦</span></a></div>
    <div class="topspaceinfo">
      <h1>邦CMS</h1>
      <p>我的生活像 拍了一出戏 有超多导演跟编剧</p>
    </div>
    <div class="about_c">
      <p>网名：邦 </p>
      <p>职业：PHPer </p>
      <p>籍贯：江西省-赣州市</p>
      <p>电话：18650680200</p>
      <p>邮箱：375500819@qq.com</p>
    </div>
    
    <div class="tj_news">
      <h2>
        <p class="tj_t1">最新文章</p>
      </h2>
      <ul>
      		
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/spjc/39.html">遇见你第4集 遇见</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/37.html">Mozilla 发布能运行在 Android 上的 Firefox OS</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/javascript/36.html">推荐十个最新免费的 jQuery 表格插件</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/qdjs/35.html"> AngularJS logo  HTML的Web框架 AngularJS</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/34.html">GitHub Atom 0.208.0 发布，文本编辑器</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/33.html">iOS 8.4 Beta 4才来，iOS 9解锁密码就升级到6位</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/cxkf/32.html">排名前十的SQL和NoSQL数据库</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/qdjs/31.html">10 个优秀的JavaScript开发框架</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/cxysh/30.html">麻省理工的 Picture 语言：代码瘦身的秘诀</a></li>
        
	
	
        	<li><a href="http://127.0.0.1/cms/cms/Static/black/PHP/29.html">PHP数组操作详解</a></li>
        
	
      </ul>
      <h2>
        <p class="tj_t2">推荐文章</p>
      </h2>
      <ul>
      			
        <li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/27.html">史上最狡猾恶意软件现身：被发现后会自爆</a></li>
        
		
        <li><a href="http://127.0.0.1/cms/cms/Static/black/cxysh/28.html">27种迹象你该考虑辞掉程序员的工作</a></li>
        
		
        <li><a href="http://127.0.0.1/cms/cms/Static/black/cxysh/30.html">麻省理工的 Picture 语言：代码瘦身的秘诀</a></li>
        
		
        <li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/33.html">iOS 8.4 Beta 4才来，iOS 9解锁密码就升级到6位</a></li>
        
		
        <li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/37.html">Mozilla 发布能运行在 Android 上的 Firefox OS</a></li>
        
		
        <li><a href="http://127.0.0.1/cms/cms/Static/black/hlwxw/34.html">GitHub Atom 0.208.0 发布，文本编辑器</a></li>
        
      </ul>
    </div>
    <div class="links">
      <h2>
        <p>友情链接</p>
      </h2>
      <ul>
      		
        <li><a href="http://111.com">11</a></li>
        
	
        <li><a href="http://www.houdunwang.com">后盾网</a></li>
        
	
        <li><a href="http://www.qq.com">腾讯QQ</a></li>
        
	
        <li><a href="http://www.baidu.com">百度</a></li>
        
	
      </ul>
    </div>
    <div class="copyright">
      <ul>
        <p> Design by <a href="/">邦CMS</a></p>
        <p></p>
        </p>
      </ul>
    </div>
  </aside>
 <!--右边结束-->
  <script src="js/silder.js"></script>
  <div class="clear"></div>
  <!-- 清除浮动 --> 
</div>
</body>
</html>
